<?php

namespace app\index\controller;
use think\facade\Hook;
use think\Db;

class User
{

    public function login()
    {
        $ret = [
            "code" => 200,
            "msg" => "OK",
            "data" => []
        ];

        if (!request()->ispost()) {
            $ret['code'] = 400;
            $ret['msg'] = '非法请求';

            return json($ret);
        }

        $account = input('post.account');
        $password = input('post.password');

        $res = Db::name('user')->where(['mobile' => $account])->field('id,mobile,nickname,real_name')->find();

        if (!$res) {
            $ret['code'] = 400;
            $ret['msg'] = '用户不存在';

            return json($ret);
        }

        $ret['data']['assess_token'] = base64_encode(md5(time()));
        $ret['data']['refresh_token'] = base64_encode(md5(time()));
        $ret['data']['user'] = $res;

        return json($ret);
    }

    // 资料
    public function profile()
    {
        $ret = [
            "code" => 200,
            "msg" => "success",
            "data" => []
        ];

        if (!request()->ispost()) {
            $ret['code'] = 400;
            $ret['msg'] = '非法请求';
            return json($ret);
        }

        $userId = input('post.userid');

        if (empty($userId)) {
            $ret['code'] = 400;
            $ret['msg'] = '用户ID不能为空';
            return json($ret);
        }

        $ret['data'] = [
            'userid' => $userId,
            'avatar' => 'https://hhbusiness.oss-cn-shenzhen.aliyuncs.com/tmp/378A78DF-56BC-5918-EC9A-4E1E17707330.png',
            'realname' => '北桥苏',
            'age' => 22,
            'login_count' => 33,
            'last_login' => '2020/07/20 18:30',
            'content' => '15465446'
        ];

        return json($ret);
    }

}
